package cn.lili.modules.promotion.serviceimpl;

import cn.lili.common.vo.PageVO;
import cn.lili.modules.promotion.entity.dos.BlindboxRule;
import cn.lili.modules.promotion.entity.vos.BlindboxRuleVO;
import cn.lili.modules.promotion.mapper.BlindboxRuleMapper;
import cn.lili.modules.promotion.service.BlindboxRelService;
import cn.lili.modules.promotion.service.BlindboxRuleService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 盲盒规则 业务实现
 * @author xiangqian
 */
@Service
@RequiredArgsConstructor
public class BlindboxRuleServiceImpl extends ServiceImpl<BlindboxRuleMapper, BlindboxRule> implements BlindboxRuleService {

    @Resource
    private BlindboxRelService blindboxRelService;

    /**
     * 获取盲盒规则列表
     *
     * @param page       分页条件
     * @param queryParam 查询条件
     */
    @Override
    public Page<BlindboxRuleVO> getBlindboxRuleList(BlindboxRuleVO queryParam, PageVO page) {
        return this.baseMapper.getBlindboxRuleList(PageUtil.initPage(page), queryParam.queryWrapper(queryParam));
    }

    /**
     * 根据商品ID查询盲盒规则
     *
     * @param goodsId 商品ID
     */
    @Override
    public BlindboxRule getBlindboxRule(String goodsId) {
        String ruleId = blindboxRelService.getRuleIdByGoodsId(goodsId);
        if(ruleId == null){
            return null;
        }
        return this.baseMapper.selectById(ruleId);
    }
}